//
//  BasePresentController.m
//  Safflower
//
//  Created by 袁亮 on 2016/11/30.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "BasePresentController.h"

@interface BasePresentController ()


@end

@implementation BasePresentController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation ==  UIInterfaceOrientationLandscapeLeft || interfaceOrientation ==  UIInterfaceOrientationLandscapeRight );
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self makeBaseControllerView];
}

- (void)makeBaseControllerView
{
    _titleView = [UIView new];
    [self.view addSubview:_titleView];
    [_titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(56);
    }];
    
    _rightBtn = [UIButton new];
    [_rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _rightBtn.layer.masksToBounds = YES;
    _rightBtn.layer.borderColor = [[UIColor blackColor] CGColor];
    _rightBtn.layer.borderWidth = 1.0;
    _rightBtn.layer.cornerRadius = 5.0;
    _rightBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    [_rightBtn addTarget:self action:@selector(clickRightButton) forControlEvents:UIControlEventTouchUpInside];
    [_titleView addSubview:_rightBtn];
    [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(14);
        make.right.mas_equalTo(-14);
        make.size.mas_equalTo(CGSizeMake(60, 28));
    }];
    
    _titleSuperView = [UIView new];
    [_titleView addSubview:_titleSuperView];
    [_titleSuperView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(84);
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(_rightBtn.mas_left).mas_offset(-12);
    }];
    
    _titleLabel = [UILabel new];
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = [UIFont systemFontOfSize:24.0f];
    [_titleSuperView addSubview:_titleLabel];
    
    _logoImage = [UIImageView new];
    _logoImage.image = [UIImage imageNamed:@"base_title_logo"];
    [_titleSuperView addSubview:_logoImage];
    
    _baseView = [UIView new];
    [self.view addSubview:_baseView];
    [_baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleView.mas_bottom).mas_offset(0);
        make.left.right.bottom.mas_equalTo(0);
    }];
    
}

- (void)setBaseTitle:(NSString *)baseTitle
{
    _titleLabel.text = baseTitle;
    
    CGSize titleSize = [UIView autoFitWithText:baseTitle withFont:[UIFont systemFontOfSize:24.0f]];
    
    [_titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(titleSize.width + 10, titleSize.height));
        make.centerY.mas_equalTo(_titleSuperView);
        make.centerX.mas_equalTo(_titleSuperView.mas_centerX).mas_offset(-30);
    }];
    
    [_logoImage mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(30, 30));
        make.centerY.mas_equalTo(_titleSuperView);
        make.left.mas_equalTo(_titleLabel.mas_right).mas_offset(10);
    }];
}

- (void)setRightBtnTitle:(NSString *)rightBtnTitle
{
    [_rightBtn setTitle:rightBtnTitle forState:UIControlStateNormal];
}

- (void)clickRightButton
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
